var api_token = sessionStorage.getItem('api_token'); //参数
// var head_title = sessionStorage.getItem('tr_name'); //景区名
var tr_id = sessionStorage.getItem('tr_id'); //景区id
var more = sessionStorage.getItem('more'); //景区id
var start_date = sessionStorage.getItem('start_date'); //开始时间
var end_date = sessionStorage.getItem('end_date'); //结束时间
var userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
// var http = 'http://c.yueshengkj.com/api/auth/mac?tr_id=' + tr_id; //请求网址
var http;
if(start_date && end_date){
    http = 'http://c.yueshengkj.com/api/auth/mac?tr_id=' + tr_id + '&start_date=' + start_date + '&end_date=' + end_date; //请求网址
} else {
    http = 'http://c.yueshengkj.com/api/auth/mac?tr_id=' + tr_id
}
function mytime() {
    var t = null;
    t = setTimeout(time, 1000); //開始运行
    function time() {
        clearTimeout(t); //清除定时器
        var dt = new Date();
        var y = dt.getFullYear();
        var mt = dt.getMonth() + 1;
        var day = dt.getDate();
        var h = dt.getHours(); //获取时
        var m = dt.getMinutes(); //获取分
        var s = dt.getSeconds(); //获取秒
        document.getElementById("showTime").innerHTML = y + "年" + mt + "月" + day + "日" + h + "时" + m + "分" + s + "秒";
        t = setTimeout(time, 1000); //设定定时器，循环运行
    }
};
function getAllData(arg) {
    var ajaxbg = $("#background,#progressBar");
    $.ajax({
        type: "GET",
        headers: {
            "Authorization":'Bearer ' + api_token//此处放置请求到的用户token
        },
        async: true,
        url: http,
        cache: false,
        crossDomain: true == !(document.all),
        data: {
            "@action": arg.join(',')
        },
        dataType: "json",
        beforeSend:function()
        {
            ajaxbg.show();
        },
        success: function (res) {
            for (var i = 0; i < arg.length; i++) {
                if (arg[i] == 'crowdCount') {
                    crowdCount(res.data.crowdCount);
                } else if (arg[i] == 'tourismResortStayTop') {
                    tourismResortStayTop(res.data.tourismResortStayTop)
                } else if (arg[i] == 'tourismAttractionTop') {
                    tourismAttractionTop(res.data.tourismAttractionTop)
                } else if (arg[i] == 'crowdFlow') {
                    crowdFlow(res.data.crowdFlow)
                } else if (arg[i] == 'information') {
                    information(res.data.information)
                } else if (arg[i] == 'hotMap') {
                    hotMap(res.data.hotMap)
                }
            }
            ajaxbg.hide();
        },
        error:function(){
            ajaxbg.hide();
            alert("数据加载失败!");
        }
    });
}
// 热力图
function hotMap(data) {
    var map = new BMap.Map("left-heatmap");  // 创建地图实例
    var point = new BMap.Point(userInfo.bindResort.longitude, userInfo.bindResort.latitude);
    // var point = new BMap.Point(116.418261,39.921984);
    var points = data;
    map.setMapStyle({style:'dark'});
    var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
    map.addControl(top_left_navigation);
    map.addControl(new BMap.MapTypeControl());
    // map.centerAndZoom(point, 15);             // 初始化地图，设置中心点坐标和地图级别
    if (userInfo.bindResort.more) {
        map.centerAndZoom(point, Number(userInfo.bindResort.more.map_range));             // 初始化地图，设置中心点坐标和地图级别   5-19
    }  else {
        map.centerAndZoom(point, 15);             // 初始化地图，设置中心点坐标和地图级别   5-19
    }
    map.enableScrollWheelZoom(); // 允许滚轮缩放
    if(!isSupportCanvas()){
        alert('热力图目前只支持有canvas支持的浏览器,您所使用的浏览器不能使用热力图功能~')
    }

    var heatmapOverlay = new BMapLib.HeatmapOverlay({"radius":30,"visible":true,"opacity":0.3});
    map.addOverlay(heatmapOverlay);
    heatmapOverlay.setDataSet({data:points,max:1000});
    //判断浏览区是否支持canvas
        var gradient = {
            0:'#0c1aff',
            .4:'#33d60d',
            .7:'#FBF81F',
            1:'#F8260D'
        };

        heatmapOverlay.setOptions({"gradient":gradient});
    function isSupportCanvas(){
        var elem = document.createElement('canvas');
        return !!(elem.getContext && elem.getContext('2d'));
    }
}

$(function () {
    mytime();
    getAllData(['hotMap']);
})